<?php

namespace app\data\model;

use app\index\exception\LogicException;
use think\admin\Model;

class ActActivityTicket extends Model
{
    protected $table = 'act_activity_ticket';

    //售卖状态
    public function getSaleStatusAttr()
    {
        $now = time();
        $begin=strtotime($this->sale_begin);
        $end = strtotime($this->sale_end);
        if($now<$begin){
            return 1;
        }elseif($now>$end){
            return 2;
        }else{
            if($this->rest=0){
                return 3;
            }else{
                return 4;
            }
        }

    }

    public function activityModel()
    {
        return $this->belongsTo(ActActivity::class,'activity_id');
    }

    public function ifOnSale()
    {
        $now = time();
        $begin=strtotime($this->sale_begin);
        $end = strtotime($this->sale_end);
        return $now>=$begin&&$now<=$end;
    }

    public function saleOneTicket()
    {
        if($this->rest<=0){
            throw new LogicException('门票已售完！');
        }
        $this->rest--;
        return $this->save();

    }

}